package com.ym.manager.admin.product;

import com.ym.manager.admin.goods.model.Goods;
import com.ym.manager.admin.product.model.Promote;
import com.ym.utils.Common;
import com.ym.utils.Convert;
import com.ym.utils.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PromoteService {

    @Autowired
    private PromoteDao promoteDao;

    /**
     * 产品集列表
     * @param pageNum
     * @param limit
     * @return
     */
    public Pages<Promote> getList(int pageNum, int limit) {
        Map<String,Object> map = new HashMap<>();
        int begin = (pageNum-1)*limit;
        map.put("begin",begin);
        map.put("limit",limit);
        List<Promote> list = promoteDao.getList(map);
        int count  = promoteDao.getListCount(map);
        Pages<Promote> pages = new Pages<>(count,pageNum,limit);
        pages.setList(list);
        return pages;
    }

    /**
     * 产品集产品列表
     * @param pageNum
     * @param limit
     * @param name
     * @param categoryId
     * @return
     */
    public Pages<Goods> getPromote(int pageNum, int limit, String name, int categoryId) {
        Map<String,Object> map = new HashMap<>();
        int begin = (pageNum-1)*limit;
        map.put("begin",begin);
        map.put("limit",limit);
        if(Common.isNotEmpty(name)){
            map.put("name",name);
        }
        if(categoryId>0){
            map.put("categoryId",categoryId);
        }
        List<Goods> list = promoteDao.getPromote(map);
        int count  = promoteDao.getPromoteCount(map);
        Pages<Goods> pages = new Pages<>(count,pageNum,limit);
        pages.setList(list);
        return pages;
    }

    /**
     * 新增产品集
     * @param params
     * @return
     */
    public int insert(Map<String,Object> params) {
        int i =promoteDao.insert(params);
        i += promoteDao.insertGoods(Convert.objToInt(params.get("id")), Convert.objToStr(params.get("promoteGoods")).split(","));
        return i;
    }

    public Promote detail(int id) {
        Promote promote = promoteDao.detail(id);
        List<String> strings = promoteDao.goodsDetail(promote.getPlaceId());
        String promoteGoods = "";
        for(int i = 0;i<strings.size();i++){
            promoteGoods += strings.get(i)+(i == strings.size()-1?"":",");
        }
        promote.setPromoteGoods(promoteGoods);
        return promote;
    }

    /**
     * 修改产品集
     * @param params
     * @return
     */
    @Transactional
    public int update(int id ,Map<String,Object> params) {
        Promote promote = promoteDao.detail(id);

        params.put("placeId_c",promote.getPlaceId());
        int i =promoteDao.update(params);
        int placeId = Convert.objToInt(promote.getPlaceId());
        promoteDao.delGoods(placeId);
        i += promoteDao.insertGoods(placeId, Convert.objToStr(params.get("promoteGoods")).split(","));
        return i;
    }

    /**
     * 修改产品集上下线状态
     * @param id
     * @param status
     * @return
     */
    public int shelf(int id, int status) {
        return promoteDao.shelf(id,status);
    }

    /**
     * 删除产品集
     * @param id
     * @return
     */
    @Transactional
    public int delete(int id) {
        int i = promoteDao.delete(id);
        return i;
    }
}
